//
//  HMUserAccountViewModel.swift
//  TJHWeiBo
//
//  Created by tianjunhui on 16/8/11.
//  Copyright © 2016年 itcast.cn. All rights reserved.
//

import UIKit

class HMUserAccountViewModel: NSObject {

    
    static let sharedTools:HMUserAccountViewModel = HMUserAccountViewModel()
    
    //用户信息对象(为防止频繁的操作沙盒)
    
    var userAccount: HMOuthModel?
    //路径
    let file = (NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true).last! as NSString).stringByAppendingPathComponent("userAccount.archiver")
    
    //accessToken
    var accessToken:String?{
        
        if userAccount?.access_token != nil {
            if userAccount?.expires_Date?.compare(NSDate())==NSComparisonResult.OrderedDescending {
                return userAccount?.access_token
            
        }else{
            return nil
        }
        
        
        }else{
            return nil
        }
    }
    
    //判断用户是否登录过
    var isLogin : Bool {
        return accessToken != nil
    }
    
    
    override init() {
        super.init()
        //给当前类身上属性赋值
        userAccount = getUserAccount()
        
    }
    
}

//保存数据
extension HMUserAccountViewModel {
    
    //保存对象
    func saveUserAccount(userAccount:HMOuthModel){
        self.userAccount = userAccount
       
        print(file)
        //保存对象
        NSKeyedArchiver.archiveRootObject(userAccount, toFile: file)
    }
    
    
    //获取对象
    func getUserAccount()-> HMOuthModel? {
        //获取对象
        let result = NSKeyedUnarchiver.unarchiveObjectWithFile(file) as? HMOuthModel
        return result
   
    }
    
    
    
}
    //请求数据
extension HMUserAccountViewModel {
        
        //获取授权  ,添加闭包
    func getUserAccount(code: String,finish:(isSuccess: Bool)-> ()){
        
            HMNetworkTools.sharedTools.oauthLoadUserAccount(code, success: { (response) in
                guard let res = response as? [String: AnyObject] else{
                    //如果失败
                    finish(isSuccess: false)
                    return
                }
                // 通过字典创建一个模型
                let userAccount = HMOuthModel(dict: res)
                
                self.getUserInfo(userAccount,finish:finish)
                
            }) { (error) in
                finish(isSuccess: false)
                print("请求失败:\(error)")
            }
            
        }
        
        //请求个人信息
        func getUserInfo(userAccount:HMOuthModel,finish:(isSuccess: Bool)-> ()){
            
            
            HMNetworkTools.sharedTools.oauthLoadUserInfo(userAccount, success: { (response) in
                //            判断是否为nil，是否可以转成字典
                guard let res = response as?[String:AnyObject] else{
                    finish(isSuccess: false)
                    return
                }
                //赋值
                userAccount.screen_name = res["screen_name"]as? String
                userAccount.avatar_large = res["avatar_large"]as? String
                print("\(userAccount)")
                
                //保存对象
                self.saveUserAccount(userAccount)
                //成功
                finish(isSuccess: true)
                
            }) { (error) in
                finish(isSuccess: false)
                print(error)
            }
        }

        
        
    
}
